----------
--[[技能书按键绑定 mBindings]]
function SpellButton_OnEnter(self)
	local id = SpellBook_GetSpellID(self:GetID());
	if not GetSpellName(id, SpellBookFrame.bookType) then return end;
	local bind1, bind2 = GetBindingKey("SPELL "..GetSpellName(id, SpellBookFrame.bookType));
	GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
	GameTooltip:SetSpell(id, SpellBookFrame.bookType);
	if bind1 then
		if bind2 then
		GameTooltip:AddLine(bind1..", "..bind2, 1, 1, 1);
		else
		GameTooltip:AddLine(bind1, 1, 1, 1);
		end
		GameTooltip:Show();
	end
	--[[if ( GameTooltip:SetSpell(id, SpellBookFrame.bookType) ) then
		self.UpdateTooltip = SpellButton_OnEnter;
	else
		self.UpdateTooltip = nil;
	end]]
end

local function Bindehtiem(self, keyOrButton)
	local mouseFocus = GetMouseFocus();
	if not ( mouseFocus and mouseFocus:GetName():match("SpellButton(%d+)") ) then return end

	if ( GetBindingByKey(keyOrButton) == "SCREENSHOT" ) then
		RunBinding("SCREENSHOT");
		return;
	end

	-- Convert the mouse button names
	if ( keyOrButton == "LeftButton" ) then
		keyOrButton = "BUTTON1";
	elseif ( keyOrButton == "RightButton" ) then
		keyOrButton = "BUTTON2";
	elseif ( keyOrButton == "MiddleButton" ) then
		keyOrButton = "BUTTON3";
	elseif ( keyOrButton == "Button4" ) then
		keyOrButton = "BUTTON4"
	elseif ( keyOrButton == "Button5" ) then
		keyOrButton = "BUTTON5"
	end

	local keyPressed = keyOrButton;

	if ( keyOrButton ) then
		if ( keyOrButton == "BUTTON1" or keyOrButton == "BUTTON2" ) then
			return;
		end
		keyPressed = keyOrButton;
	else
		keyPressed = keyOrButton;
	end

	if ( keyPressed == "UNKNOWN" ) then
		return;
	end

	if ( keyPressed == "LSHIFT" or
	     keyPressed == "RSHIFT" or
	     keyPressed == "LCTRL" or
	     keyPressed == "RCTRL" or
	     keyPressed == "LALT" or
	     keyPressed == "RALT" ) then
		return;
	end

	if ( IsShiftKeyDown() ) then
		keyPressed = "SHIFT-"..keyPressed;
	end
	if ( IsControlKeyDown() ) then
		keyPressed = "CTRL-"..keyPressed;
	end
	if ( IsAltKeyDown() ) then
		keyPressed = "ALT-"..keyPressed;
	end


	local spellName = GetSpellName(SpellBook_GetSpellID(mouseFocus:GetID()), SpellBookFrame.bookType);
	if spellName then
		if ( keyOrButton == "ESCAPE" ) then
			local key1, key2 = GetBindingKey("SPELL "..spellName);
			if key1 then
				SetBinding(key1);
				DEFAULT_CHAT_FRAME:AddMessage(key1.." |c00b3b3b3已解除绑定|r "..spellName);
			end
			if key2 then
				SetBinding(key2);
				DEFAULT_CHAT_FRAME:AddMessage(key2.." |c00b3b3b3已解除绑定|r "..spellName);
			end
		else
			local oldAction = GetBindingAction(keyPressed);
			if oldAction ~= "" then
				DEFAULT_CHAT_FRAME:AddMessage(keyPressed.." |c00b3b3b3已解除绑定|r "..oldAction);
			end
			SetBindingSpell(keyPressed, spellName);
			DEFAULT_CHAT_FRAME:AddMessage(keyPressed.." |c00b3b3b3已绑定到|r "..spellName);
		end
		SpellButton_OnEnter(mouseFocus);
		SaveBindings(2);

	end
end

local keyFrame = CreateFrame("Button", nil, SpellBookFrame);
 keyFrame:EnableKeyboard();
 keyFrame:Hide();
 keyFrame:SetScript("OnKeyDown", Bindehtiem);

local bindBut = CreateFrame("CheckButton", "bindBut", SpellBookFrame, "UICheckButtonTemplate");
 bindButText:SetText("enable bindings");
 if (GetLocale()=="zhCN") then  bindButText:SetText("技能按键绑定"); elseif (GetLocale() == "zhTW") then  bindButText:SetText("技能按鍵綁定");end
 bindBut:SetPoint("TOPLEFT", 74, -38);
 ShowAllSpellRanksCheckBox:ClearAllPoints();
 ShowAllSpellRanksCheckBox:SetPoint("LEFT", bindButText, "RIGHT", 10, 0);
 bindBut:SetWidth(26);
 bindBut:SetHeight(26);
 bindBut:SetScript("OnMouseUp", function()
	if bindBut:GetChecked() then
		keyFrame:Hide();
	else
		keyFrame:Show();
	end
 end);
 bindBut:SetScript("OnEnter", function(self)
 GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
 GameTooltip:SetText("Click to start/stop binding abilities.\nMouseover an ability and press a button to bind them together.\nPress esc to clear all bindings for an ability.");
if (GetLocale() == "zhCN") then	 GameTooltip:SetText("点击开始/停止绑定设置。\n鼠标移动到技能上按下按键设置绑定。\n按Esc键清除按键绑定。");
elseif (GetLocale() == "zhTW") then  GameTooltip:SetText("點擊開始/停止綁定設置。\n滑鼠移動到技能上按下按鍵設置綁定。\n按Esc鍵清除按鍵綁定。");end
 GameTooltip:Show();
 end);
 bindBut:SetScript("OnLeave", function(self)
 GameTooltip:Hide();
 end);

 for i=1, 12 do
	_G["SpellButton"..i]:SetScript("OnMouseDown", function(self, keyPressed)
		if keyFrame:IsShown() then
		if ( keyPressed == "MiddleButton" ) then
			keyPressed = "BUTTON3";
		elseif ( keyPressed == "Button4" ) then
			keyPressed = "BUTTON4"
		elseif ( keyPressed == "Button5" ) then
			keyPressed = "BUTTON5"
		else
			return;
		end

		if ( IsShiftKeyDown() ) then
			keyPressed = "SHIFT-"..keyPressed;
		end
		if ( IsControlKeyDown() ) then
			keyPressed = "CTRL-"..keyPressed;
		end
		if ( IsAltKeyDown() ) then
			keyPressed = "ALT-"..keyPressed;
		end
		local spellName = GetSpellName(SpellBook_GetSpellID(self:GetID()), SpellBookFrame.bookType);
		if spellName then
			if ( keyOrButton == "ESCAPE" ) then
				local key1, key2 = GetBindingKey("SPELL "..spellName);
				local mBindTextF = " |c00b3b3b3已解除绑定|r ";
				local mBindTextT = " |c00b3b3b3已绑定到|r ";
				if key1 then
					SetBinding(key1);
					DEFAULT_CHAT_FRAME:AddMessage(key1.." |c00b3b3b3已解除绑定|r "..spellName);
				end
				if key2 then
					SetBinding(key2);
					DEFAULT_CHAT_FRAME:AddMessage(key2.." |c00b3b3b3已解除绑定|r "..spellName);
				end
			else
				local oldAction = GetBindingAction(keyPressed);
				if oldAction ~= "" then
					DEFAULT_CHAT_FRAME:AddMessage(keyPressed.." |c00b3b3b3已解除绑定|r "..oldAction);
				end
				SetBindingSpell(keyPressed, spellName);
				DEFAULT_CHAT_FRAME:AddMessage(keyPressed.." |c00b3b3b3已绑定到|r "..spellName);
			end
		SpellButton_OnEnter(self);
		SaveBindings(2);
		end
		end
	end)
 end
